package com.huiquan.synonymy.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.huiquan.framework.base.EnhancedBaseDao;
import com.huiquan.synonymy.domain.Synonymy;
import com.ibatis.sqlmap.client.SqlMapClient;

@Repository
public class SynonymyDao extends EnhancedBaseDao<Synonymy> {

	@Resource(name = "sqlMapClient")
	private SqlMapClient sqlMapClient;

	public static final String space = "SYNONYMY.";

	public SynonymyDao() {
		super(space);
	}

	public void delete(Long id) {
		Synonymy synonymy = new Synonymy();
		synonymy.setId(id);
		this.delete(synonymy);
	}

	@SuppressWarnings("unchecked")
	public List<Synonymy> retrieveListByIds(String idsStr) {
		Map<String, Object> param = new HashMap<>();
		param.put("idsStr", idsStr);
		return (List<Synonymy>) this.list(space + "RETRIEVE_LIST_BY_IDS", param, sqlMapClient);
	}

	public void deleteByIds(String idsStr) {
		Map<String, Object> param = new HashMap<>();
		param.put("idsStr", idsStr);
		this.delete(space + "DELETE_BY_IDS", param, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<Synonymy> retrieveRepeatWord(String id, String word, String property, Integer type) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		param.put("word", word);
		param.put("property", property);
		param.put("type", type);
		return (List<Synonymy>) this.list(space + "RETRIEVE_REPEAT_WORD", param, sqlMapClient);
	}

	public void batchInsert(Integer type, List<Synonymy> synonymList, String property, Long userId, String realName) {
		Map<String, Object> param = new HashMap<>();
		param.put("property", property);
		param.put("type", type);
		param.put("userId", userId);
		param.put("userName", realName);
		int start = 0, end, size = synonymList.size();
		List<Synonymy> tempList;

		while (start < size) {
			if (start + 2000 < size) {
				end = start + 2000;
			} else {
				end = size;
			}
			tempList = synonymList.subList(start, end);
			param.put("list", tempList);
			this.insert(space + "BATCH_INSERT", param, sqlMapClient);
			start += 2000;
		}
	}

	public void changeStd(long id, String std) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		param.put("std", std);

		this.update(space + "CHANGE_STD", param, sqlMapClient);
	}

	public void changeChineseStd(long id, String std) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		param.put("std", std);

		this.update(space + "CHANGE_CHINESE_STD", param, sqlMapClient);
	}
	
	public void changeEnglishStd(long id, String std) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		param.put("std", std);

		this.update(space + "CHANGE_ENGLISH_STD", param, sqlMapClient);
	}
	public List<Synonymy> retrieveListByWords(Integer type, String property, List<String> words) {
		Map<String, Object> param = new HashMap<>();
		param.put("existWords", words);
		param.put("property", property);
		param.put("type", type);

		return (List<Synonymy>) this.retrieveList(param);
	}

	public void initStdById(Long id) {
		this.update(space + "INIT_STD_BY_ID", id, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<Synonymy> retrieveWrongStd() {
		return (List<Synonymy>) this.list(space + "RETRIEVE_WRONG_STD", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, String>> retrieveOOaStdConflict() {
		return (List<Map<String, String>>) this.list(space + "RETRIEVE_O_OA_STD_CONFLICT", sqlMapClient);
	}
	
	@SuppressWarnings("unchecked")
	public List<Synonymy> retrieveByStd(String std) {
		Map<String, Object> param = new HashMap<>();
		param.put("std", std);
		return (List<Synonymy>) this.list(space + "RETRIEVE_BY_STD", param, sqlMapClient);
	}
	@SuppressWarnings("unchecked")
	public List<Synonymy> checkSynonymyConflict(Map<String, Object> param) {
		return (List<Synonymy>) this.list(space + "CHECK_SYNONYMY_CONFLICT", param, sqlMapClient);
	}
	@SuppressWarnings("unchecked")
	public List<Synonymy> checkSynonymyConflict2(Map<String, Object> param) {
		return (List<Synonymy>) this.list(space + "CHECK_SYNONYMY_CONFLICT2", param, sqlMapClient);
	}
	@SuppressWarnings("unchecked")
	public List<Map<String, String>> checkSynonymyTQuantizationConflict() {
		return (List<Map<String, String>>) this.list(space + "CHECK_T_QUANTIZATION_CONFLICT",  sqlMapClient);
	}
	@SuppressWarnings("unchecked")
	public List<String> checkRSynonymyStd() {
		return (List<String>) this.list(space + "CHECK_R_SYNONYMY_STD", sqlMapClient);
	}
}
